// pages/analysis_result/analysis_result.js
var Charts = require("../../../../utils/wxcharts-min.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    touchHandler:[],
    series: [],
    categories: [],
    categoriesX:[],
    seriesX:[],
    index:0,
    overflow:false,


    //后台数据
    simulation:[
      {time:"2014.09.24",values:"362",state:"正常"},
      { time: "2015.09.24", values: "390", state: "正常" },
      { time: "2016.09.24", values: "375", state: "正常" },
      { time: "2017.09.24", values: "360", state: "正常" },
      { time: "2018.09.24", values: "360", state: "正常" },
      { time: "2019.09.24", values: "370", state: "正常" },
      { time: "2020.09.24", values: "300", state: "正常" },
      { time: "2021.09.24", values: "500", state: "正常" },
      { time: "2022.09.24", values: "350", state: "正常" },
      { time: "2023.09.24", values: "390", state: "正常" },
      { time: "2024.09.24", values: "300", state: "正常" },
      { time: "2025.09.24", values: "660", state: "正常" },
      { time: "2026.09.24", values: "888", state: "正常" },
      { time: "2027.09.24", values: "80", state: "正常" },
      { time: "2028.09.24", values: "3", state: "正常" },
      { time: "2029.09.24", values: "362", state: "正常" },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that = this
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      backgroundColor: bgc
    })
    //获取总长度和当前页面index
    var str = options.str
    var contrastArr = JSON.parse(str)
    var simulation = contrastArr[options.index]
    // console.log(simulation)
    that.setData({ idx: options.index, length: options.length, contrastArr: contrastArr, simulation: simulation })


    //绘制canvas
    // var simulation = that.data.simulation
    var categoriesX = that.data.categoriesX
    var seriesX = that.data.seriesX
    var categories = that.data.categories
    var series = that.data.series
    categories.length = simulation.length
    series.length = simulation.length
    for (let i = 0, len = simulation.length ; i< len;i++){
      categories[i] = simulation[i].time
      series[i] = simulation[i].description
    }
    var index = that.data.index
    if (simulation.length >= 4 ){
      for (var i = index; i < index + 4; i++) {
        categoriesX[i] = categories[i]
        seriesX[i] = series[i]
      }
    }else{
      for (var i = index; i < index + simulation.length; i++) {
        categoriesX[i] = categories[i]
        seriesX[i] = series[i]
      }
    }
   
    console.log(categoriesX, seriesX)
    var min = that.getMin(seriesX) - 50
    var max = that.getMax(seriesX) - 0 + 50
    this.drawCanvas(categoriesX, seriesX,min,max)
  },
  //最大值
  getMax:function(arr1){
    var arr = Array.from(arr1)
    for(let i = 0 , len = arr.length ; i < len ; i++){
      if((i+1) < len){
        if (arr[i] - arr[i + 1] <= 0) {
          arr.splice(i, 1)
          i--
          len--
        } else {
          arr.splice(i + 1, 1)
          i--
          len--
        }
      }
      if(len == 1){
        return arr[0]
      }
    }
  },
  getMin:function(arr1){
    var arr = Array.from(arr1)
    for (let i = 0, len = arr.length; i < len; i++) {
      if ((i + 1) < len) {
        if (arr[i] - arr[i + 1] >= 0) {
          arr.splice(i, 1)
          i--
          len--
        } else {
          arr.splice(i + 1, 1)
          i--
          len--
        }
      }
      if (len == 1) {
        return arr[0]
      }
    }
  },
  //绘制canvas
  drawCanvas: function (categoriesX, seriesX,min,max){
    new Charts({
      canvasId: 'area',
      type: 'area',
      categories: categoriesX,
      dataLabel:true,
      extra: {
        lineStyle: 'curve', //线条的形状（弧形）
      },
      series: [{
        name:"点击查看更多",
        color: '#ffb332',
        data: seriesX,
        
      }],
      yAxis: {
        disabled:false,
        min:min,
        max:max,
        format: function (val) {
          return  val.toFixed() ;
        }
      },
      width: 350,
      height: 200,
    });
  },
  //点击
  changClick:function(e){
    // console.log(e)
    var that = this
    var categories = that.data.categories
    var categoriesX = that.data.categoriesX
    var series = that.data.series
    var seriesX = that.data.seriesX
    var index = that.data.index
    var touchX = that.data.touchX
    if(e.detail.y > 64 && e.detail.y < 270){
      if(e.detail.x > 210 && e.detail.x < 360){
        index++
        if (index >= categories.length - 4) {
          index = categories.length - 4
        }
        var j = index
        // console.log(j, categories, series)
        for (var i = 0; i < 4; i++) {
          categoriesX[i] = categories[j]
          seriesX[i] = series[j]
          j++
        }
        that.setData({
          index: index,
        })
      }else if(e.detail.x < 200 && e.detail.x > 55){
        index--
        if (index <= 0) {
          index = 0
        }
        var j = index
        for (var i = 0; i < 4; i++) {
          categoriesX[i] = categories[j]
          seriesX[i] = series[j]
          j++
        }
        that.setData({
          index: index,
        })
      }
      var min = that.getMin(seriesX) - 50
      var max = that.getMax(seriesX) - 0 + 50
      this.drawCanvas(categoriesX, seriesX, min, max)
      this.drawCanvas(categoriesX, seriesX,min,max)
    }
  },
  //移动
  onTouch(e) {
    // console.log(e)
    var that = this
    let touchX = e.touches[0].x,
      touchY = e.touches[0].y;
    that.setData({
      touchX: touchX
    })
  },

	onTouchMove(e) {
 
  },

	onTouchEnd(e) {
    // console.log(e)
    // this.options.touchingX = false;
    // return this.draw( this.options );
    var that = this
    var categories = that.data.categories
    var categoriesX = that.data.categoriesX
    var series = that.data.series
    var seriesX = that.data.seriesX
    var index = that.data.index
    var touchX = that.data.touchX

    //向左
    // console.log(e.changedTouches[0].x, touchX)
    if (e.changedTouches[0].x - touchX > 40) {
      // console.log(1)
      index--
      if (index <= 0) {
        index = 0
      }
     
      var j = index
      // console.log(j, categories, series)
      for (var i = 0; i < 4; i++) {
        categoriesX[i] = categories[j]
        seriesX[i] = series[j]
        j++
      }
      that.setData({
        index: index,
      })
    } else if (e.changedTouches[0].x - touchX < -40) {
      // console.log(2)
      index++
      if (index >= categories.length - 4) {
        index = categories.length - 4
      }
      var j = index
      for (var i = 0; i < 4; i++) {
        categoriesX[i] = categories[j]
        seriesX[i] = series[j]
        j++
      }
      that.setData({
        index: index,
      })
    }
    var min = that.getMin(seriesX) - 50
    var max = that.getMax(seriesX) - 0 + 50
    this.drawCanvas(categoriesX, seriesX, min, max)
    this.drawCanvas(categoriesX, seriesX,min,max)
  },
  //上一项
  lastClick:function(e){
    var that = this
    var idx = that.data.idx
    var overflow = true
    idx--
    that.setData({
      overflow: overflow,
      idx:idx
    })
    wx.showLoading({
      title: '加载中',
    })
    var simulation = that.data.simulation
    var categoriesX = that.data.categoriesX
    var seriesX = that.data.seriesX
    var categories = that.data.categories
    var series = that.data.series
    categories.length = simulation.length
    series.length = simulation.length
    for (let i = 0, len = simulation.length; i < len; i++) {
      categories[i] = simulation[i].time
      series[i] = simulation[i].values
    }
    var index = that.data.index
    var j = index
    for (var i = 0; i < 4; i++) {
      categoriesX[i] = categories[j]
      seriesX[i] = series[j]
      j++
    }
    var min = that.getMin(seriesX) - 50
    var max = that.getMax(seriesX) - 0 + 50
    this.drawCanvas(categoriesX, seriesX, min, max)
    setTimeout(function () {
      wx.hideLoading()
      that.setData({ overflow: false })
    }, 3000)
  },
  //下一项
  nextClick:function(e){
    var that = this
    var idx = that.data.idx
    var overflow = true
    idx++
    that.setData({
      overflow: overflow,
      idx: idx
    })
    wx.showLoading({
      title: '加载中',
    })
    var contrastArr = that.data.contrastArr
    var simulation = contrastArr[idx]
    console.log(simulation)
    var categoriesX = that.data.categoriesX
    var seriesX = that.data.seriesX
    var categories = that.data.categories
    var series = that.data.series
    categories.length = simulation.length
    series.length = simulation.length
    for (let i = 0, len = simulation.length; i < len; i++) {
      categories[i] = simulation[i].time
      series[i] = simulation[i].description
    }
    var index = that.data.index
    console.log(index)
    var j = index
   
    if (simulation.length >= 4) {
      for (var i = 0; i < 4; i++) {
        categoriesX[i] = categories[j]
        seriesX[i] = series[j]
        j++
      }
    } else {
      for (var i = 0; i < simulation.length; i++) {
        categoriesX[i] = categories[j]
        seriesX[i] = series[j]
        j++
      }
    }
    console.log(categoriesX, seriesX)
    var min = that.getMin(seriesX) - 50
    var max = that.getMax(seriesX) - 0 + 50
    this.drawCanvas(categoriesX, seriesX, min, max)
    setTimeout(function(){
      wx.hideLoading()
      that.setData({ overflow:false})
    },3000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})